import sdk from '../index';
import gameCfg from '../../gameCfg';
import { quality } from '../../shared/face/enums';

/**
 * 副本状态
 */
export enum DungeonStatus {
    关闭 = 'closed',
    开启 = 'open',
    冷却 = 'cooldown'
}

/**
 * 副本信息
 */
export interface DungeonInfo {
    mapName: string;
    difficulty: string;
    status: DungeonStatus;
    openTime: number;
    closeTime: number;
    cooldownEndTime: number;
}

/**
 * 副本管理器
 */
export class DungeonManager {
    private static instance: DungeonManager;
    private currentDungeon: DungeonInfo | null = null;
    private lastUpdateTime: number = 0;

    private constructor() {
        this.startDungeonCycle();
    }

    public static getInstance(): DungeonManager {
        if (!DungeonManager.instance) {
            DungeonManager.instance = new DungeonManager();
        }
        return DungeonManager.instance;
    }

    /**
     * 启动副本循环
     */
    private startDungeonCycle(): void {
        // 立即开始第一个副本
        this.startNewDungeon();
        
        // 设置定时器，每分钟检查一次
        setInterval(() => {
            this.updateDungeonStatus();
        }, 60000); // 每分钟检查一次
    }

    /**
     * 开始新的副本
     */
    private startNewDungeon(): void {
        const now = Date.now();
        const openDuration = gameCfg.dungeon.openDuration * 60 * 1000; // 转换为毫秒
        const cooldownDuration = gameCfg.dungeon.cooldownDuration * 60 * 1000; // 转换为毫秒
        
        // 随机选择地图和难度
        const randomMap = gameCfg.dungeon.availableMaps[Math.floor(Math.random() * gameCfg.dungeon.availableMaps.length)];
        const randomDifficulty = gameCfg.dungeon.difficulties[Math.floor(Math.random() * gameCfg.dungeon.difficulties.length)];
        this.currentDungeon = {
            mapName: randomMap,
            difficulty: randomDifficulty,
            status: DungeonStatus.开启,
            openTime: now,
            closeTime: now + openDuration,
            cooldownEndTime: now + openDuration + cooldownDuration
        };
        
        // console.log(`🌍 新副本已开启: ${randomMap} (${randomDifficulty})`);
    }

    /**
     * 更新副本状态
     */
    private updateDungeonStatus(): void {
        if (!this.currentDungeon) {
            this.startNewDungeon();
            return;
        }

        const now = Date.now();
        
        if (this.currentDungeon.status === DungeonStatus.开启 && now >= this.currentDungeon.closeTime) {
            // 副本时间到，进入冷却
            this.currentDungeon.status = DungeonStatus.冷却;
            // console.log(`🌍 副本已关闭: ${this.currentDungeon.mapName} (${this.currentDungeon.difficulty})`);
        } else if (this.currentDungeon.status === DungeonStatus.冷却 && now >= this.currentDungeon.cooldownEndTime) {
            // 冷却时间到，开始新副本
            this.startNewDungeon();
        }
    }

    /**
     * 获取当前副本信息
     */
    public getCurrentDungeon(): DungeonInfo | null {
        this.updateDungeonStatus();
        return this.currentDungeon;
    }

    /**
     * 获取副本状态描述
     */
    public getDungeonStatusText(): string {
        const dungeon = this.getCurrentDungeon();
        if (!dungeon) {
            return '暂无副本信息';
        }

        const now = Date.now();
        
        if (dungeon.status === DungeonStatus.开启) {
            const remainingTime = Math.max(0, dungeon.closeTime - now);
            const minutes = Math.floor(remainingTime / (60 * 1000));
            const seconds = Math.floor((remainingTime % (60 * 1000)) / 1000);
            return `🌍 副本开启中: ${dungeon.mapName} (${dungeon.difficulty})\n⏳ 剩余时间: ${minutes}分${seconds}秒`;
        } else if (dungeon.status === DungeonStatus.冷却) {
            const remainingCooldown = Math.max(0, dungeon.cooldownEndTime - now);
            const minutes = Math.floor(remainingCooldown / (60 * 1000));
            const seconds = Math.floor((remainingCooldown % (60 * 1000)) / 1000);
            return `🌍 副本冷却中\n⏳ 下次开启: ${minutes}分${seconds}秒`;
        } else {
            return '🌍 副本状态异常';
        }
    }

    /**
     * 检查是否可以进入副本
     */
    public canEnterDungeon(): boolean {
        const dungeon = this.getCurrentDungeon();
        return dungeon !== null && dungeon.status === DungeonStatus.开启;
    }

    /**
     * 获取当前副本的完整信息
     */
    public getDungeonInfo(): {
        name: string;
        difficulty: string;
        status: string;
        remainingTime: number;
        cooldownTime: number;
    } {
        const dungeon = this.getCurrentDungeon();
        const now = Date.now();
        
        if (!dungeon) {
            return {
                name: '无',
                difficulty: '无',
                status: '无',
                remainingTime: 0,
                cooldownTime: 0
            };
        }

        const remainingTime = dungeon.status === DungeonStatus.开启 ? 
            Math.max(0, dungeon.closeTime - now) : 0;
        const cooldownTime = dungeon.status === DungeonStatus.冷却 ? 
            Math.max(0, dungeon.cooldownEndTime - now) : 0;

        return {
            name: dungeon.mapName,
            difficulty: dungeon.difficulty,
            status: dungeon.status,
            remainingTime,
            cooldownTime
        };
    }

    /**
     * 测试方法 - 用于调试
     */
    public testDungeonSystem(): void {
        console.log('=== 副本系统测试 ===');
        console.log('当前副本信息:', this.getDungeonInfo());
        console.log('是否可以进入副本:', this.canEnterDungeon());
        console.log('副本状态文本:', this.getDungeonStatusText());
        console.log('==================');
    }
}

export default DungeonManager.getInstance(); 